thrift应用举例(c/c++作为服务端、java作为客户端)

您所在的位置:网站首页 thrift例子 student thrift应用举例(c/c++作为服务端、java作为客户端)

thrift应用举例(c/c++作为服务端、java作为客户端)

#thrift应用举例(c/c++作为服务端、java作为客户端)| 来源: 网络整理| 查看: 265

阅读更多

最近做的一个项目,后端服务是c++写的,因所有参与这个项目的同事除了me之外,他们都不会c/c++语言。没有办法,我就承担了这个有意思的任务。下面通过实战例子,来剖析thrift的应用。

目录:

1.thrift是干什么用的?

      2.thrift语法?

      3.实战例子

            3.1 环境

            3.2 安装

            3.3 实战例子

      4.小结

 

     1.Thrift是干什么用的?

      Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 这些编程语言间无缝结合的、高效的服务。Thrift最初由facebook开发,07年四月开放源码,08年5月进入apache孵化器。Thrift允许你定义一个简单的定义文件中的数据类型和服务接口,以作为输入文件,编译器生成代码用来方便地生成RPC客户端和服务器通信的无缝跨编程语言。

    2.Thrift语法

请参考网上其他文章。网上很多,不在赘述。重点了解:基本类型、容器、结构体、枚举、服务即可。

      2.1基本类型:

            bool: 布尔值 (true or false), one byte

            byte: 有符号字节

            i16: 16位有符号整型

            i32: 32位有符号整型

            i64: 64位有符号整型

            double: 64位浮点型

            string: Encoding agnostic text or binary string

            Note that: Thrift不支持无符号整型,因为Thrift目标语言没有无符号整型,无法转换

      2.2容器:

            Thrift容器与流行编程语言的容器类型相对应,采用Java泛型风格。它有3种可用容器类型:

                  list: 元素类型为t1的有序表,容许元素重复。

                  set:元素类型为t1的无序表,不容许元素重复。

                  map: 键类型为t1,值类型为t2的kv对,键不容许重复。

      容器中元素类型可以是除了service外的任何合法Thrift类型(包括结构体和异常)。

      2.3 枚举和结构体

            枚举,和我们平常理解的枚举一样。

            这里只强调结构体就是类似于c/c++中的结构体(几乎一模一样)。类似于java中的实体bean。例子如下:

      枚举:

            enum TweetType {

                 TWEET,       // (1)

               RETWEET = 2, // (2)

                DM = 0xa,    // (3)

              REPLY

            }

 

  结构体:

            struct TxtClass{

                   1: string text,

                   2: string classId,

                   3: double score,

            }

      2.4 服务

      在流行的序列化/反序列化框架(如protocal buffer)中,Thrift是少有的提供多语言间RPC服务的框架。这是Thrift的一大特色。Thrift编译器会根据选择的目标语言为server产生服务接口代码,为client产生stubs。

这里就是thrift自动会生成的方法,我们的服务端也需要器生成的方法中进行编码。例子如下:

            service Twitter {

                      // A method definition looks like C code. It has a return type, arguments,

                      // and optionally a list of exceptions that it may throw. Note that argument

                      // lists and exception list are specified using the exact same syntax as

                      // field lists in structs.

                      void ping(),                                    // (1)

                      bool postTweet(1:Tweet tweet);                  // (2)

                      TweetSearchResult searchTweets(1:string query); // (3)

 

                      // The 'oneway' modifier indicates that the client only makes a request and

                      // does not wait for any response at all. Oneway methods MUST be void.

                      oneway void zip()                               // (4)

                }

 

1.         3.实战例子

      3.1 环境

            centOS6.4 64位操作系统

      3.2 安装

            首先,从官网http://thrift.apache.org/上下载thrift-0.8.0版本,你可以下载最新的版本。

            其次,从网上下载ant 和ivy。放在/usr目录下。

            Ant下载路径:http://ant.apache.org/

            Ivy下载路径:http://ant.apache.org/ivy/

      一切准备就绪,即可安装:

           3.2.1安装ant和ivy(root用户)

第一步:安装 切复制ivy-2.2.0.jar到/usr/local/apache-ant-1.8.2/lib/目录中

       

# tar xzvf apache-ant-1.8.2-bin.tar.gz -C /usr/local # tar xzvf apache-ivy-2.2.0-bin-with-deps.tar.gz -C /usr/local # cp /usr/local/apache-ivy-2.2.0/ivy-2.2.0.jar /usr/local/apache-ant-1.8.2/lib/  

 

          第二步:编辑vim  /etc/ profile ,添加如下两行

        

export ANT_HOME=/usr/local/apache-ant-1.8.2 PATH=$ANT_HOME/bin:$PATH  

 

      第三步:解压thrift压缩文件。我用的是thrift-0.8.0.tar.gz

             

tar zxvf thrift-0.8.0.tar.gz 进入到thrift-0.8.0目录

 

   第四步:包的检查、安装(如果缺少包,请安装缺少的包)

       

./configure --prefix=/usr/local/ --with-boost=/usr/local --without-php make make install

 

   第五步:

   在终端输入:thrift –version  查看是否安装成功。

 

3.3 实战例子

特别注意:如下例子:演示的是thrift-0.7.0版本(因我服务器上安装的此版本)

    1.定义thrift文件:user.thrift     

struct User{ 1: string uid, 2: string uname, 3: bool usex, 4: i16 uage, } service UserService{ void add(1: User u), User get(1: string uid), }  

 

2.通过thrift的shell工具命令生成c++,java代码框架 

 

thrift -r --gen cpp user.thrift thrift -r --gen java user.thrift    通过执行以上命令:会生成子目录gen-cpp,gen-java。      3.通过执行如下命令,生成C/C++服务端代码 cp gen-cpp/UserService_server.skeleton.cpp UserServer.cpp        4.修改服务端的代码,如下所示       // This autogenerated skeleton file illustrates how to build a server. // You should copy it to another filename to avoid overwriting it. #include #include #include "UserService.h" #include #include #include #include #include #include #include #include #include using namespace ::apache::thrift; using namespace ::apache::thrift::protocol; using namespace ::apache::thrift::transport; using namespace ::apache::thrift::server; using namespace ::apache::thrift::concurrency; using boost::shared_ptr; using namespace std; class UserServiceHandler : virtual public UserServiceIf { public: UserServiceHandler() { // Your initialization goes here } void add(const User& u) { // Your implementation goes here cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3